package com.zjs.express.controller;

import com.zjs.express.bean.Admin;
import com.zjs.express.bean.Express;
import com.zjs.express.bean.User;
import com.zjs.express.bean.vo.ExpressVo;
import com.zjs.express.common.ResultModel;
import com.zjs.express.mvc.annotation.RequestMapping;
import com.zjs.express.mvc.annotation.ResponseBody;
import com.zjs.express.service.ExpressService;
import com.zjs.express.service.impl.ExpressServiceImpl;
import com.zjs.express.utils.RandomUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Random;


public class ExpressController {
    private ExpressService expressService = new ExpressServiceImpl();

    @RequestMapping("/admin/views/express/list.do")
    @ResponseBody
    public ResultModel<List<ExpressVo>> list(HttpServletRequest request, HttpServletResponse response) {
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        return expressService.list(Integer.valueOf(pageIndex), Integer.valueOf(pageSize));
    }

    @RequestMapping("/admin/views/express/add.do")
    @ResponseBody
    public ResultModel add(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        //区分1是管理员录入或者0是快递员录入
        String type = request.getParameter("type");
        Express express = new Express();
        express.setNumber(number);
        express.setCompany(company);
        express.setUsername(username);
        express.setUserPhone(userPhone);
        express.setStatus(0);
        express.setCode(RandomUtils.random());
        //获取当前登录人的手机号
        if (type.equals("1")) {
            Object adminObj = request.getSession().getAttribute("admin");
            if (adminObj != null && adminObj instanceof Admin) {
                Admin admin = (Admin) adminObj;
                express.setSysPhone(admin.getPhone());
            }
        } else {
            Object userObj = request.getSession().getAttribute("user");
            if (userObj != null && userObj instanceof User) {
                User user = (User) userObj;
                express.setSysPhone(user.getPhone());
            }
        }
        return expressService.add(express);
    }

    @RequestMapping("/admin/views/express/listByNumber.do")
    @ResponseBody
    public ResultModel<ExpressVo> listByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        return expressService.listByNumber(number);
    }

    @RequestMapping("/admin/views/express/update.do")
    @ResponseBody
    public ResultModel update(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String status = request.getParameter("status");
        //区分1是管理员录入或者0是快递员录入
        String type = request.getParameter("type");
        Express express = new Express();
        express.setId(id);
        express.setNumber(number);
        express.setCompany(company);
        express.setUsername(username);
        express.setUserPhone(userPhone);
        express.setStatus(0);
        express.setCode(RandomUtils.random());
        //获取当前登录人的手机号
        if (type.equals("1")) {
            Object adminObj = request.getSession().getAttribute("admin");
            if (adminObj != null && adminObj instanceof Admin) {
                Admin admin = (Admin) adminObj;
                express.setSysPhone(admin.getPhone());
            }
        } else {
            Object userObj = request.getSession().getAttribute("user");
            if (userObj != null && userObj instanceof User) {
                User user = (User) userObj;
                express.setSysPhone(user.getPhone());
            }
        }
        return expressService.update(express);
    }


}
